package tyk.Servlet;

import tyk.Service.OderItemService;
import tyk.pojo.AccountPage;
import tyk.pojo.OrderItem;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/***
 *@title $NAME
 *@description <TODO description class purpose>
 *@author Tang'ya'kang
 *@VERSION 1.0.0
 *@CREATE 2023/5/18 17:40
 **/
public class OrderItemsServlet extends HttpServlet {
    public void SelectItemsByOrder_id(HttpServletRequest request,HttpServletResponse response) throws SQLException, InstantiationException, IllegalAccessException, NoSuchFieldException, ServletException, IOException {
        System.out.println("到此一游");
        String order_id=request.getParameter("order_id");
        OderItemService oderItemService=new OderItemService();
        List<OrderItem> list=new ArrayList<>();
        list=oderItemService.SelectItemsByOrder_id(order_id);
        request.setAttribute("OrderItems",list);
        System.out.println(list);
        request.getRequestDispatcher("./html/MyOrderItems.jsp").forward(request,response);
    }
    public void SelectItemsByMerchan_id(HttpServletRequest request,HttpServletResponse response) throws SQLException, InstantiationException, IllegalAccessException, NoSuchFieldException, ServletException, IOException {
        Integer merchan_id= Integer.valueOf(request.getParameter("merchan_id"));
        Integer Page= Integer.valueOf(request.getParameter("Page"));
        List<OrderItem> list=new ArrayList<>();
        OderItemService oderItemService=new OderItemService();
        AccountPage accountPage=new AccountPage();
        accountPage.setPage(Page);
        list=oderItemService.SelectItemsByMerchan_id(merchan_id,accountPage);
        request.setAttribute("OrderItems",list);
        request.setAttribute("AccountPage",accountPage);
        request.setAttribute("url","http://localhost:8080/JaveWeb/orderServlet?action=SelectOrder&user_id="+merchan_id+"&user_name=notroot&Page=");
        request.getRequestDispatcher("./manage/MarchanOrders.jsp").forward(request,response);
    }
    public void SendOutGoods(HttpServletRequest request,HttpServletResponse response) throws SQLException, IOException {
        Integer id= Integer.valueOf(request.getParameter("id"));
        Integer value= Integer.valueOf(request.getParameter("value"));
        OderItemService oderItemService=new OderItemService();
        if(id!=-1) oderItemService.SendOutGoods(id,value);
        else {
            String order_id=request.getParameter("order_id");
            oderItemService.SendOutGoods(id,value,order_id);
        }

        response.sendRedirect(request.getHeader("referer"));
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action=request.getParameter("action");
        try {
            Method method = this.getClass().getDeclaredMethod(action,HttpServletRequest.class,HttpServletResponse.class);
            method.invoke(this,request,response);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
